#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include "unity.h"
#include "fifo_IntHandler.h"
#include "mock_eth_commom.h"


// 测试设置和清理函数
void setUp(void)
{
    // 初始化全局变量
    memset(&Eth_status, 0, sizeof(Eth_status));
    Fifo_InstancePtr = NULL;
}

void tearDown(void)
{
    // 测试清理
}

// 测试用例1: 接收完成中断 - 工作模式
void test_fifo_IntHandler_ReceiveComplete_WorkMode(void)
{
    // Arrange
    XLlFifo mockFifo;
    u32 pending_value = XLLF_INT_RC_MASK;

    // 设置期望调用
    XLlFifo_IntPending_ExpectAndReturn(&mockFifo, pending_value);
    B1553_get_current_mode_ExpectAndReturn(SYS_MODE_WORK);

    XLlFifo_RxReset_Expect(&mockFifo);

    XLlFifo_IntClear_Expect(&mockFifo, XLLF_INT_RC_MASK);

    XLlFifo_IntPending_ExpectAndReturn(&mockFifo, 0);

    XIntc_Acknowledge_Expect(&Intc, XPAR_INTC_0_LLFIFO_0_VEC_ID);
    XIntc_Acknowledge_Expect(&Intc, XPAR_INTC_0_LLFIFO_1_VEC_ID);
    XLlFifo_IntClear_Expect(&mockFifo, 0);

    // Act
    fifo_IntHandler(&mockFifo);

    // Assert
    // 验证状态未被更新（工作模式下应该丢弃数据）
    TEST_ASSERT_EQUAL_INT(0, Eth_status.rx_flag);
    TEST_ASSERT_EQUAL_INT(0, Eth_status.FramesRx);
}

// 测试用例2: 接收完成中断 - 非工作模式
void test_fifo_IntHandler_ReceiveComplete_OtherMode(void) {
    // Arrange
    XLlFifo mockFifo;
    u32 pending_value = XLLF_INT_RC_MASK;

    // 设置期望调用
    XLlFifo_IntPending_ExpectAndReturn(&mockFifo, pending_value);
    B1553_get_current_mode_ExpectAndReturn(SYS_MODE_IDLE);

    XLlFifo_IntClear_Expect(&mockFifo, XLLF_INT_RC_MASK);

    XLlFifo_IntPending_ExpectAndReturn(&mockFifo, 0);

    XIntc_Acknowledge_Expect(&Intc, XPAR_INTC_0_LLFIFO_0_VEC_ID);
    XIntc_Acknowledge_Expect(&Intc, XPAR_INTC_0_LLFIFO_1_VEC_ID);
    XLlFifo_IntClear_Expect(&mockFifo, 0);

    // Act
    fifo_IntHandler(&mockFifo);

    // Assert
    // 验证状态被正确更新
    TEST_ASSERT_EQUAL_INT(1, Eth_status.rx_flag);
    TEST_ASSERT_EQUAL_INT(1, Eth_status.FramesRx);
}

// 测试用例3: 发送完成中断
void test_fifo_IntHandler_TransmitComplete(void) {
    // Arrange
    XLlFifo mockFifo;
    u32 pending_value = XLLF_INT_TC_MASK;

    // 设置期望调用
    XLlFifo_IntPending_ExpectAndReturn(&mockFifo, pending_value);
    XLlFifo_IntClear_Expect(&mockFifo, XLLF_INT_TC_MASK);
    XLlFifo_IntPending_ExpectAndReturn(&mockFifo, 0);
    XIntc_Acknowledge_Expect(&Intc, XPAR_INTC_0_LLFIFO_0_VEC_ID);
    XIntc_Acknowledge_Expect(&Intc, XPAR_INTC_0_LLFIFO_1_VEC_ID);
    XLlFifo_IntClear_Expect(&mockFifo, 0);

    // Act
    fifo_IntHandler(&mockFifo);

    // Assert
    TEST_ASSERT_EQUAL_INT(1, Eth_status.FramesTx);
}

// 测试用例4: 错误中断
void test_fifo_IntHandler_ErrorInterrupt(void) {
    // Arrange
    XLlFifo mockFifo;
    u32 error_code = 0xA2000000;  
    u32 pending_value = 0xA20000F0;

    // 设置期望调用
    XLlFifo_IntPending_ExpectAndReturn(&mockFifo, pending_value);
    XLlFifo_IntClear_Expect(&mockFifo, XLLF_INT_ERROR_MASK);

    XLlFifo_IntPending_ExpectAndReturn(&mockFifo, 0);

    XIntc_Acknowledge_Expect(&Intc, XPAR_INTC_0_LLFIFO_0_VEC_ID);
    XIntc_Acknowledge_Expect(&Intc, XPAR_INTC_0_LLFIFO_1_VEC_ID);
    XLlFifo_IntClear_Expect(&mockFifo, 0);

    // Act
    fifo_IntHandler(&mockFifo);

    // Assert
    TEST_ASSERT_EQUAL_UINT32(error_code, Eth_status.FifoErrCode);
}

// 测试用例5: 其他中断
void test_fifo_IntHandler_OtherInterrupt(void) {
    // Arrange
    XLlFifo mockFifo;
    u32 pending_value = 0x00000100;  // 其他中断类型

    // 设置期望调用
    XLlFifo_IntPending_ExpectAndReturn(&mockFifo, pending_value);
    XLlFifo_IntClear_Expect(&mockFifo, pending_value);
    XLlFifo_IntPending_ExpectAndReturn(&mockFifo, 0);
    XIntc_Acknowledge_Expect(&Intc, XPAR_INTC_0_LLFIFO_0_VEC_ID);
    XIntc_Acknowledge_Expect(&Intc, XPAR_INTC_0_LLFIFO_1_VEC_ID);
    XLlFifo_IntClear_Expect(&mockFifo, 0);

    // Act
    fifo_IntHandler(&mockFifo);

    // Assert - 主要验证函数调用顺序正确
}

// 测试用例6: 多个中断同时发生
void test_fifo_IntHandler_MultipleInterrupts(void) {
    // Arrange
    XLlFifo mockFifo;
    u32 pending_value1 = XLLF_INT_RC_MASK;
    u32 pending_value2 = XLLF_INT_TC_MASK;
    u32 pending_value3 = 0;

    // 设置期望调用序列
    XLlFifo_IntPending_ExpectAndReturn(&mockFifo, pending_value1);
    B1553_get_current_mode_ExpectAndReturn(SYS_MODE_IDLE);
    XLlFifo_IntClear_Expect(&mockFifo, XLLF_INT_RC_MASK);

    XLlFifo_IntPending_ExpectAndReturn(&mockFifo, pending_value2);

    XLlFifo_IntClear_Expect(&mockFifo, XLLF_INT_TC_MASK);

    XLlFifo_IntPending_ExpectAndReturn(&mockFifo, pending_value3);

    XIntc_Acknowledge_Expect(&Intc, XPAR_INTC_0_LLFIFO_0_VEC_ID);
    XIntc_Acknowledge_Expect(&Intc, XPAR_INTC_0_LLFIFO_1_VEC_ID);
    XLlFifo_IntClear_Expect(&mockFifo, 0);

    // Act
    fifo_IntHandler(&mockFifo);

    // Assert
    TEST_ASSERT_EQUAL_INT(1, Eth_status.rx_flag);
    TEST_ASSERT_EQUAL_INT(1, Eth_status.FramesRx);
    TEST_ASSERT_EQUAL_INT(1, Eth_status.FramesTx);
}

// 测试用例7: 无中断情况
void test_fifo_IntHandler_NoInterrupt(void) {
    // Arrange
    XLlFifo mockFifo;
    u32 pending_value = 0;

    // 设置期望调用
    XLlFifo_IntPending_ExpectAndReturn(&mockFifo, pending_value);
    XIntc_Acknowledge_Expect(&Intc, XPAR_INTC_0_LLFIFO_0_VEC_ID);
    XIntc_Acknowledge_Expect(&Intc, XPAR_INTC_0_LLFIFO_1_VEC_ID);
    XLlFifo_IntClear_Expect(&mockFifo, 0);

    // Act
    fifo_IntHandler(&mockFifo);

    // Assert
    TEST_ASSERT_EQUAL_INT(0, Eth_status.rx_flag);
    TEST_ASSERT_EQUAL_INT(0, Eth_status.FramesRx);
    TEST_ASSERT_EQUAL_INT(0, Eth_status.FramesTx);
    TEST_ASSERT_EQUAL_UINT32(0, Eth_status.FifoErrCode);
}